/** 
 * 文 件 名:HealthApplicationContext.java
 * Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
 * 创 建 人:Administrator
 * 日    期:2012-4-13 上午11:22:02
 * 修 改 人: 
 * 日   期:
 * 修改描述:
 * 描   述:TODO
 */
package net.seehealth.healthpad.application.base;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import net.seehealth.healthpad.dataaccess.HealthSqliteHelper;

import android.app.Application;
import android.content.Context;

/**
 * @className HealthApplicationContext
 * @author 林洪波
 * @description 自定义application
 * @date 2012-4-13 上午11:22:02
 */
public class HealthApplicationContext extends Application {
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Application#onCreate()
	 */
	@Override
	public void onCreate() {
		System.out.println("---------开始");
		// TODO Auto-generated method stub
		super.onCreate();
		initCopyDataBase();
	}

	// 初始化database到/data/data/files/seehealth
	private void initCopyDataBase() {
		// 判断数据库有没存在
		File database = this.getFileStreamPath(HealthSqliteHelper.DATABASENAME);
		if (!database.exists()) {
			// 获取数据库文件输出流
			FileOutputStream os = null;
			try {
				os = this.openFileOutput(HealthSqliteHelper.DATABASENAME,
						Context.MODE_PRIVATE);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			InputStream is = null;
			try {
				is = this.getAssets().open(HealthSqliteHelper.DATABASENAME);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			byte[] buffer = new byte[8192];
			int count = 0;
			try {
				while ((count = is.read(buffer)) > 0) {
					os.write(buffer, 0, count);
					os.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				is.close();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
